﻿using System.Linq;
using System.Web.Mvc;
using QDT.Common;
using QDT.Core.Domain;
using QDT.Core.Experience;
using QDT.Core.Projects;
using QDT.Web.Code;
using QDT.Web.Code.Attributes;
using QDT.Web.Models;
using QDT.Core.Assignments;

namespace QDT.Web.Controllers
{
    [HttpsRequire]
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            ViewBag.ParentCount = DbProvider.D<ParentProject>().Count();
            ViewBag.Banner  = DbProvider.D<Article>().Where(x => x.CateId == 4 || x.Cate.Title.Contains("Banner")).OrderBy(x=>x.Sort).ToList();
            return View();
        }

        public PartialViewResult LoginInfo()
        {
            var isLogin = QDTContext.Current.IsAuthenticated;
            return PartialView("_LoginInfo", new IsLoginModel(isLogin, isLogin ? QDTContext.Current.User : null));
        }

        public ActionResult ProjectList(int? page, int safeclass = 0, int deadline = 0, int status = 0)
        {

            ViewBag.safeclass = safeclass;
            ViewBag.deadline = deadline;
            ViewBag.status = status;
            const int pageSize = 9;
            var pageNumber = page ?? 1;

            var filter = PredicateExtensionses.True<Project>();
            var user = QDTContext.Current.User;

            if (user == null || user.GroupID != 1) {
                filter = filter.And(x => x.TypeID != 7);
            }
            if (status != 0) 
            {
                filter = filter.And(x => x.InnerStatus == status);
            }
            if (safeclass != 0)
            {
                filter = filter.And(x => x.TypeID == safeclass);
            }
            if (deadline == 1)
            {
                filter = filter.And(x => x.RepayTime >= deadline && x.RepayTime < 4);
            }
            else if (deadline == 4)
            {
                filter = filter.And(x => x.RepayTime >= deadline && x.RepayTime < 6);
            }
            else if (deadline == 6)
            {
                filter = filter.And(x => x.RepayTime >= deadline && x.RepayTime <= 12);
            }
            var porject = DbProvider.D<Project>().Where(filter.And(x => !x.IsDelete && x.IsEffect && x.StartTime.HasValue && !x.IsOffline)).OrderByDescending(x => x.ID);
            return View(porject.ToPagedList(pageNumber - 1, pageSize));
        }

        public ActionResult AssignmentList(int? page, int safeclass = 0, int deadline = 0, int status = 0)
        {
            ViewBag.safeclass = safeclass;
            ViewBag.deadline = deadline;
            ViewBag.status = status;
            const int pageSize = 9;
            var pageNumber = page ?? 1;
            var filter = PredicateExtensionses.True<Assignment>();
            if (status == 0)
            {
                filter = filter.And(x => x.InnerStatus == 1 || x.InnerStatus == 2);
            }
            else
            {
                filter = filter.And(x => x.InnerStatus == status);
            }
            if (safeclass == 0)
            {
                filter = filter.And(x => x.Project.TypeID == 2 || x.Project.TypeID == 3);
            }
            else
            {
                filter = filter.And(x => x.Project.TypeID == safeclass);
            }
            if (deadline == 1)
            {
                filter = filter.And(x => x.Project.RepayTime >= deadline && x.Project.RepayTime < 4);
            }
            if (deadline == 4)
            {
                filter = filter.And(x => x.Project.RepayTime >= deadline && x.Project.RepayTime < 6);
            }
            if (deadline == 6)
            {
                filter = filter.And(x => x.Project.RepayTime >= deadline && x.Project.RepayTime <= 12);
            }
            var assignments = DbProvider.D<Assignment>().Where(filter.And(x => !x.IsDelete && x.IsEffect && !x.IsOffline)).OrderBy(x=>x.InnerStatus).ThenByDescending(x => x.ID);
            return View(assignments.ToPagedList(pageNumber - 1,pageSize));
        }
        public ActionResult ExperienceList(int? page, int expclass = 0)
        {
            const int pageSize = 9;
            var pageNumber = page ?? 1;

            var filter = PredicateExtensionses.True<ExpProject>();

            var expproject = DbProvider.D<ExpProject>().Where(filter.And(x => x.Status != 0)).OrderBy(x => x.Status).ThenByDescending(x => x.ID).ToPagedList(pageNumber - 1, pageSize);

            return View(expproject);
        }
        public ActionResult Agency(int aid)
        {
            var agency = DbProvider.GetByID<ProjectAgency>(aid);
            if (agency == null) return Error("担保公司不存在");

            var images = DbProvider.D<ProjectImage>().Where(x => x.AgencyId == agency.ID).OrderByDescending(x => x.Sort).ThenBy(x => x.ID).ToList();
            ViewData["Images"] = images;

            var projects = DbProvider.D<Project>().Count(x => x.InnerStatus == 1 && x.Parent.AgencyId == agency.ID);
            ViewData["Projects"] = projects;

            return View(agency);
        }

        public ActionResult Guide() {
            return View();
        }

        public ActionResult Help() {
            return View();
        }

        public ActionResult Contact() {
            return View();
        }

        public ActionResult About(string p) {
            return View("About");
        }

        public ActionResult Service() {
            return View();
        }

        public ActionResult Guarantee() {
            return View();
        }

        public ActionResult Loan() {
            return View();
        }


        public ActionResult Banks() {
            return View();
        }

        public ActionResult Data()
        {
            return View();
        }

        public ActionResult Risk()
        {
            return View();
        }

        public ActionResult Income()
        {
            return View();
        }

        public ActionResult Moneyseat()
        {
            return View();
        }
        public ActionResult Dataseat()
        {
            return View();
        }

        public ActionResult MMoneyFree()
        {
            return View();
        }
    
        public ActionResult Activity()
        {
            return View();
        }

        public ActionResult Sitemap()
        {
            return View();
        }

    }
}
